﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Evolution
{
    class Cel
    {
        protected int row, column;
        
        /// <summary>
        /// Symbol, which class are printing
        /// </summary>
        public char Symbol { get; protected set; }

        /// <summary>
        /// Сколько прожила клетка
        /// </summary>
        public int Level { get; protected set; }

        public Cel()
        {
            Level = 0;
        }

        public Cel(int Row, int Column)
        {
            Level = 0;
            Symbol = ' ';
            row = Row;
            column = Column;
        }

        /// <summary>
        /// Что происходит с клеткой за 1 ход
        /// </summary>
        /// <param name="evol"></param>
        /// <param name="curCel"></param>
        public virtual void OneStep(Evol evol, ref Cel curCel)
        {
            int maxRowIndex, minRowIndex, maxColumnIndex, minColumnIndex;
            maxRowIndex = Math.Min(evol.Size - 1, row + 1);
            minRowIndex = Math.Max(0, row - 1);
            maxColumnIndex = Math.Min(evol.Size - 1, column + 1);
            minColumnIndex = Math.Max(0, column - 1);
            int countPredator, countVegetarian = countPredator = 0;

            for (int i = minRowIndex; i <= maxRowIndex; i++)
                for (int j = minColumnIndex; j <= maxColumnIndex; j++)
                {
                    if (i == row && j == column) continue;

                    if (evol[i, j] is Predator) countPredator++;
                    else if (evol[i, j] is Vegetarian) countVegetarian++;
                }

            if (countPredator == countVegetarian && countVegetarian >= MyConst.NEEDFORVEGETARIAN &&
                countPredator > MyConst.NEEDFORPREDATOR)
            {
                Random R = new Random();
                int r = R.Next(0, 101);
                if (r <= 33) //Predator
                    curCel = new Predator(row, column);
                else if (r <= 66) //Vegetarian
                    curCel = new Vegetarian(row, column);
                else
                    curCel.IncLevel();
            }
            else if (countPredator >= countVegetarian && countPredator >= MyConst.NEEDFORPREDATOR)
                curCel = new Predator(row, column);
            else if (countPredator <= countVegetarian && countVegetarian >= MyConst.NEEDFORVEGETARIAN)
                curCel = new Vegetarian(row, column);            
            else if (countPredator >= MyConst.NEEDFORPREDATOR)
                curCel = new Predator(row, column);
            else if (countVegetarian >= MyConst.NEEDFORVEGETARIAN)
                curCel = new Vegetarian(row, column);
            else
                curCel.IncLevel();
        }

        /// <summary>
        /// Увеличивает счетчик жизни
        /// </summary>
        protected void IncLevel() {Level++;}

        /// <summary>
        /// Вывести на экран клетку
        /// </summary>
        public virtual void Show()
        {
            Console.Write(Symbol);

        }

        /// <summary>
        /// Записать клетку в потом
        /// </summary>
        /// <param name="file"></param>
        public void WriteToStream(StreamWriter file)
        {
                file.Write(Symbol);
        }

        /// <summary>
        /// Копирование клетки (создание нового экземпляра)
        /// </summary>
        /// <returns></returns>
        public virtual Cel CopyTo()
        {
            Cel tmp = new Cel(row, column);
            tmp.Level = Level;
            return tmp;
        }
    }
}
